/** 
 * projectName:  
 * fileName:  
 * packageName:  
 * date: 
 * copyright(c) 2017-2020 xxx公司
 */
package cn.message.service.impl;

import cn.itcast.mapper.userMapper;
import cn.itcast.pojo.*;
import cn.message.service.evaluateService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import cn.itcast.pojo.evaluateExample;

/**
 * @Description: ${后台管理用户评价serviceImpl}
 * @return ${return_type}
 * @throws
 * @author 徐威
 * @date 2019/6/17 9:21
 */

@Slf4j
@Service
public class MessageevaluateServiceImpl implements evaluateService {
    @Resource
    cn.itcast.mapper.evaluateMapper evaluateMapper;
    @Resource
    cn.itcast.mapper.userMapper userMapper;
    //1.获取所有商品评论
    public List<evaluate> getAllEvaluate() {
        evaluateExample uEx=new evaluateExample();
        evaluateExample.Criteria criteria=uEx.createCriteria();
        criteria.getAllCriteria();
        return  evaluateMapper.selectByExample(uEx);

    }


    //2.查找评价
    public List<evaluate> getEvaluate(Integer productId) {
        evaluateExample uEx=new evaluateExample();
        evaluateExample.Criteria criteria=uEx.createCriteria();
        criteria.andProductIdEqualTo(productId);
        return  evaluateMapper.selectByExample(uEx);

    }

    //3.删除评价
    @Override
    public boolean delectEvaluate(Integer evaluateId) {
        boolean isdelect = true;
        //判断传入字段是否为空
        if (StringUtils.isNotBlank(ObjectUtils.toString(evaluateId))) {
            evaluate evaluate = evaluateMapper.selectByPrimaryKey(evaluateId);
            if (StringUtils.isNotBlank(ObjectUtils.toString(evaluateId))) {
                evaluateMapper.deleteByPrimaryKey(evaluateId);
            } else {
                isdelect = false;
                log.debug("商品评价存在");
            }
        } else {
            isdelect = false;
            log.debug("evaluateId为空");
        }
        return isdelect;
    }

    @Override
    public List<evaluate> findAllProduct(Integer pageNum, Integer pageSize, String select, String value) {
        //分页
        PageHelper.startPage(pageNum, pageSize);
        evaluateExample uEx=new evaluateExample();
        evaluateExample.Criteria criteria=uEx.createCriteria();
        if(select.equals("evaluateId")){
            criteria.andEvaluateIdEqualTo(Integer.parseInt(value));
        }else  if(select.equals("productId")){
            criteria.andProductIdEqualTo(Integer.parseInt(value));
        }else  if(select.equals("orderId")){
            criteria.andOrderIdEqualTo(Integer.parseInt(value));
        }
        return  evaluateMapper.selectByExample(uEx);
    }

    @Override
    public List<user> findAllUser(Integer pageNum, Integer pageSize, String select, String value) {
        //分页
        PageHelper.startPage(pageNum, pageSize);
        userExample uEx=new userExample();
        userExample.Criteria criteria=uEx.createCriteria();
        if(select.equals("nickName")){
            criteria.andNickNameLike("%"+value+"%");
        }
        return  userMapper.selectByExample(uEx);
    }
}
